home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_3.5 / Examples / Reaction / ListBrowser / ListBrowserExample.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-11-02  |  14.3 KB  |  526 lines

  1.  
  2. /**
  3. *
  4. * COPYRIGHT:
  5. *
  6. *   Unless otherwise noted, all files are Copyright (c) 1999 Amiga, Inc.
  7. *   All rights reserved.
  8. *
  9. * DISCLAIMER:
  10. *
  11. *   This software is provided "as is". No representations or warranties
  12. *   are made with respect to the accuracy, reliability, performance,
  13. *   currentness, or operation of this software, and all use is at your
  14. *   own risk. Neither Amiga nor the authors assume any responsibility
  15. *   or liability whatsoever with respect to your use of this software.
  16. *
  17.  
  18.  **  ListBrowserExample.c -- List Browser class test.
  19.  **
  20.  **  This is a simple example testing some of the capabilities of the
  21.  **  ListBrowser gadget class.
  22.  **
  23.  **  This is a good example how to use Reaction classes like simple BOOPSI
  24.  **  classes and how to use it without window.class and the full ReAction
  25.  **  environment and layouter.
  26.  **
  27.  **  This code opens a simple window and then a ListBrowser gadget which is
  28.  **  subsequently attached to the window's gadget list.  Everytime the user
  29.  **  clicks on the close gadget, this code changes some of the attributes
  30.  **  of the ListBrowser gadget to demonstrate different ways it can be used,
  31.  **  including one demonstration which creates two images using the Label
  32.  **  class and shows them in the ListBrowser.
  33.  **
  34.  **/
  35.  
  36.  
  37. #include <exec/types.h>
  38. #include <exec/memory.h>
  39. #include <intuition/intuition.h>
  40. #include <intuition/gadgetclass.h>
  41. #include <intuition/imageclass.h>
  42. #include <intuition/icclass.h>
  43. #include <utility/tagitem.h>
  44. #include <gadgets/listbrowser.h>
  45. #include <images/label.h>
  46.  
  47. #include <proto/dos.h>
  48. #include <proto/exec.h>
  49. #include <proto/intuition.h>
  50. #include <proto/listbrowser.h>
  51. #include <proto/label.h>
  52.  
  53. #include <clib/alib_protos.h>
  54.  
  55. #include <string.h>
  56. #include <stdio.h>
  57.  
  58. #define RAWKEY_CURSORUP 76
  59. #define RAWKEY_CURSORDOWN 77
  60. #define QUALIFIER_SHIFT 0x03
  61. #define QUALIFIER_ALT 0x30
  62. #define QUALIFIER_CTRL 0x08
  63.  
  64.  
  65. /* Function prototypes.
  66.  */
  67. BOOL make_list(struct List *, UBYTE **, LONG *);
  68. VOID free_list(struct List *);
  69. VOID wait_for_close(struct Window *, struct Gadget *);
  70.  
  71. /* Global variables.
  72.  */
  73. struct Library *ListBrowserBase;
  74. struct Library *LabelBase;
  75.  
  76.  
  77. UBYTE *col1[] =
  78. {
  79.     "This is a", "test of the", "ListBrowser", "gadget class.",
  80.     "This is like", "a souped-up", "listview", "gadget.  It", "has many",
  81.     "cool new", "features", "though like", "multiple", "columns,",
  82.     "horizontal", "scrolling,", "images in", "nodes,", "columns titles",
  83.     "and much much", "more!",
  84.     "This is a", "test of the", "ListBrowser", "gadget class.",
  85.     "This is like", "a souped-up", "listview", "gadget.  It", "has many",
  86.     "cool new", "features", "though like", "multiple", "columns,",
  87.     "horizontal", "scrolling,", "images in", "nodes,", "columns titles",
  88.     "and much much", "more!",
  89.     "This is a", "test of the", "ListBrowser", "gadget class.",
  90.     "This is like", "a souped-up", "listview", "gadget.  It", "has many",
  91.     "cool new", "features", "though like", "multiple", "columns,",
  92.     "horizontal", "scrolling,", "images in", "nodes,", "columns titles",
  93.     "and much much", "more!", NULL
  94. };
  95.  
  96. LONG col2[] =
  97. {
  98.     1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
  99.     15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
  100.     27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
  101.     39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
  102.     51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62,
  103.     63
  104. };
  105.  
  106. struct ColumnInfo ci[] =
  107. {
  108.     { 80, "Col 1", 0 },
  109.     { 60, "Col 2", 0 },
  110.     { 60, "Col 3", 0 },
  111.     { -1, (STRPTR)~0, -1 }
  112. };
  113.  
  114. struct ColumnInfo fancy_ci[] =
  115. {
  116.     { 100, NULL, 0 },
  117.     { -1, (STRPTR)~0, -1 }
  118. };
  119.  
  120. /* Some fonts for our fancy list.
  121.  */
  122. struct TextAttr helvetica24b = { (STRPTR)"helvetica.font", 24, FSF_BOLD, FPF_DISKFONT };
  123. struct TextAttr times18i = { (STRPTR)"times.font", 18, FSF_ITALIC, FPF_DISKFONT };
  124. struct TextAttr times18 = { (STRPTR)"times.font", 18, 0, FPF_DISKFONT };
  125.  
  126.  
  127. /* This is the start of our programme.
  128.  */
  129. main()
  130. {
  131.     struct Screen *screen = NULL;
  132.  
  133.     /* We'll just open up on the default public screen, and use its screen font.
  134.      */
  135.     if (screen = LockPubScreen(NULL))
  136.     {
  137.         struct Window *win = NULL;
  138.  
  139.         /* Open the window, note how we size the window to perfectly fit
  140.          * all the gadgets.
  141.          */
  142.         if (win = OpenWindowTags(NULL,
  143.             WA_Left, 0,
  144.             WA_Top, screen->Font->ta_YSize + 3,
  145.             WA_Width, 300,
  146.             WA_Height, 160,
  147.             WA_CustomScreen, screen,
  148.             WA_IDCMP, IDCMP_GADGETUP | IDCMP_MOUSEMOVE | IDCMP_RAWKEY |
  149.                         IDCMP_CLOSEWINDOW | IDCMP_GADGETDOWN,
  150.             WA_Flags, WFLG_DRAGBAR | WFLG_DEPTHGADGET | WFLG_CLOSEGADGET |
  151.                         WFLG_SIZEGADGET | WFLG_ACTIVATE | WFLG_SMART_REFRESH,
  152.             WA_Title, "ListBrowser Class Demo",
  153.             WA_MinWidth, 50,
  154.             WA_MinHeight, 50,
  155.             WA_MaxWidth, -1,
  156.             WA_MaxHeight, -1,
  157.             TAG_DONE))
  158.         {
  159.             PutStr("Creating ListBrowser class\n");
  160.             if (ListBrowserBase = OpenLibrary("gadgets/listbrowser.gadget", 44))
  161.             {
  162.                 struct Gadget *listbrowser_gad;
  163.                 struct List label_list;
  164.  
  165.                 make_list(&label_list, col1, col2);
  166.  
  167.                 /* Create a listbrowser gadget.
  168.                  */
  169.                 PutStr("Creating ListBrowser object\n");
  170.                 if (listbrowser_gad = (struct Gadget *)NewObject(LISTBROWSER_GetClass(), NULL,
  171.                                                         GA_ID, 1,
  172.                                                         GA_Top, win->BorderTop + 5,
  173.                                                         GA_Left, 10,
  174.                                                         GA_RelWidth, -34,
  175.                                                         GA_RelHeight, -(win->BorderTop + win->BorderBottom + 10),
  176.                                                         GA_RelVerify, TRUE,
  177.                                                         LISTBROWSER_Labels, (ULONG)&label_list,
  178.                                                         LISTBROWSER_ColumnInfo, (ULONG)&ci,
  179.                                                         LISTBROWSER_ColumnTitles, TRUE,
  180.                                                         LISTBROWSER_MultiSelect, FALSE,
  181.                                                         LISTBROWSER_Separators, TRUE,
  182.                                                         LISTBROWSER_ShowSelected, FALSE,
  183.                                                         LISTBROWSER_Editable, TRUE,
  184.                                                         TAG_END))
  185.                 {
  186.                     /* Adding gadgets.
  187.                      */
  188.                     PutStr("Adding gadget\n");
  189.                     AddGList(win, listbrowser_gad, -1, -1, NULL);
  190.                     PutStr("Refreshing gadget\n");
  191.                     RefreshGList(listbrowser_gad, win, NULL, -1);
  192.  
  193.                     /* Wait for close gadget click to continue.
  194.                      */
  195.                     SetWindowTitles(win, "<- Click here to continue", (UBYTE *)~0);
  196.                     wait_for_close(win, listbrowser_gad);
  197.  
  198.                     /* Make Visible
  199.                      */
  200.                     SetWindowTitles(win, "Make Visible 10", (UBYTE *)~0);
  201.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  202.                         LISTBROWSER_MakeVisible, 10,
  203.                         LISTBROWSER_EditNode, 8,
  204.                         LISTBROWSER_EditColumn, 1,
  205.                         TAG_DONE);
  206.                     ActivateGadget(listbrowser_gad, win, NULL);
  207.                     wait_for_close(win, listbrowser_gad);
  208.  
  209.                     /* Show selected
  210.                      */
  211.                     SetWindowTitles(win, "Show selected, Auto-Fit", (UBYTE *)~0);
  212.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  213.                         LISTBROWSER_ShowSelected, TRUE,
  214.                         LISTBROWSER_AutoFit, TRUE,
  215.                         LISTBROWSER_HorizontalProp, TRUE,
  216.                         TAG_DONE);
  217.                     wait_for_close(win, listbrowser_gad);
  218.  
  219.                     /* Multi-select
  220.                      */
  221.                     SetWindowTitles(win, "Multi-select, Virtual Width of 500", (UBYTE *)~0);
  222.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  223.                         LISTBROWSER_MultiSelect, TRUE,
  224.                         LISTBROWSER_VirtualWidth, 500,
  225.                         LISTBROWSER_AutoFit, FALSE,
  226.                         TAG_DONE);
  227.                     wait_for_close(win, listbrowser_gad);
  228.  
  229.                     /* Detach the list.
  230.                      */
  231.                     SetWindowTitles(win, "Detached list", (UBYTE *)~0);
  232.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  233.                         LISTBROWSER_MultiSelect, FALSE,
  234.                         LISTBROWSER_Labels, ~0,
  235.                         TAG_DONE);
  236.                     wait_for_close(win, listbrowser_gad);
  237.  
  238.                     /* No separators, no title, 1 column.
  239.                      */
  240.                     SetWindowTitles(win, "No separators, no title, 1 column.", (UBYTE *)~0);
  241.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  242.                         LISTBROWSER_Labels, (ULONG)&label_list,
  243.                         LISTBROWSER_ColumnInfo, (ULONG)&fancy_ci,
  244.                         LISTBROWSER_Separators, FALSE,
  245.                         LISTBROWSER_ColumnTitles, FALSE,
  246.                         LISTBROWSER_AutoFit, TRUE,
  247.                         LISTBROWSER_VirtualWidth, 0,
  248.                         TAG_DONE);
  249.                     wait_for_close(win, listbrowser_gad);
  250.  
  251.                     /* Fancy list.
  252.                      */
  253.                     PutStr("Creating Label class\n");
  254.                     if (LabelBase = OpenLibrary("images/label.image", 44))
  255.                     {
  256.                         struct List fancy_list;
  257.                         struct Image *image1, *image2;
  258.                         struct Node *node1, *node2;
  259.  
  260.                         NewList(&fancy_list);
  261.  
  262.                         PutStr("Creating Label object\n");
  263.                         if (image1 = (struct Image *)NewObject(LABEL_GetClass(), NULL,
  264.                                                         IA_FGPen, 1,
  265.                                                         IA_BGPen, 2,
  266.                                                         IA_Font, (ULONG)&helvetica24b,
  267.                                                         LABEL_Text, (ULONG)"R",
  268.                                                         IA_Font, (ULONG)×18i,
  269.                                                         LABEL_Text, (ULONG)"e ",
  270.                                                         IA_Font, (ULONG)&helvetica24b,
  271.                                                         LABEL_Text, (ULONG)"Act",
  272.                                                         IA_Font, (ULONG)×18i,
  273.                                                         LABEL_Text, (ULONG)"ion",
  274.                                                         TAG_END))
  275.                         {
  276.                             PutStr("Creating Label object\n");
  277.                             if (image2 = (struct Image *)NewObject(LABEL_GetClass(), NULL,
  278.                                                             IA_FGPen, 2,
  279.                                                             IA_BGPen, 0,
  280.                                                             IA_Font, (ULONG)×18,
  281.                                                             LABEL_Text, (ULONG)"Amiga Inc.",
  282.                                                             TAG_END))
  283.                             {
  284.                                 if (node1 = AllocListBrowserNode(1,
  285.                                                 LBNA_Column, 0,
  286.                                                     LBNCA_Image, (ULONG)image1,
  287.                                                     LBNCA_Justification, LCJ_CENTRE,
  288.                                                 TAG_DONE))
  289.                                 {
  290.                                     AddTail(&fancy_list, node1);
  291.  
  292.                                     if (node2 = AllocListBrowserNode(1,
  293.                                                     LBNA_Column, 0,
  294.                                                         LBNCA_Image, (ULONG)image2,
  295.                                                         LBNCA_Justification, LCJ_CENTRE,
  296.                                                     TAG_DONE))
  297.                                     {
  298.                                         AddTail(&fancy_list, node2);
  299.  
  300.                                         /* Set listbrowser.
  301.                                          */
  302.                                         SetWindowTitles(win, "Fancy", (UBYTE *)~0);
  303.                                         SetGadgetAttrs(listbrowser_gad, win, NULL,
  304.                                             LISTBROWSER_ColumnInfo, &fancy_ci,
  305.                                             LISTBROWSER_Labels, (ULONG)&fancy_list,
  306.                                             LISTBROWSER_AutoFit, TRUE,
  307.                                             TAG_DONE);
  308.  
  309.                                         wait_for_close(win, listbrowser_gad);
  310.  
  311.                                         SetGadgetAttrs(listbrowser_gad, win, NULL,
  312.                                             LISTBROWSER_Labels, ~0,
  313.                                             TAG_DONE);
  314.  
  315.                                         FreeListBrowserNode(node2);
  316.                                     }
  317.                                     FreeListBrowserNode(node1);
  318.                                 }
  319.                                 DisposeObject(image2);
  320.                             }
  321.                             DisposeObject(image1);
  322.                         }
  323.  
  324.                         PutStr("Closing Label class\n");
  325.                         CloseLibrary(LabelBase);
  326.                     }
  327.                     else
  328.                         PutStr("ERROR: Couldn't create Label class\n");
  329.  
  330.                     /* Read-only
  331.                      */
  332.                     SetWindowTitles(win, "Read-only", (UBYTE *)~0);
  333.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  334.                         LISTBROWSER_ColumnInfo, (ULONG)&ci,
  335.                         LISTBROWSER_Labels, (ULONG)&label_list,
  336.                         LISTBROWSER_AutoFit, TRUE,
  337.                         LISTBROWSER_Selected, -1,
  338.                         GA_ReadOnly, TRUE,
  339.                         TAG_DONE);
  340.                     wait_for_close(win, listbrowser_gad);
  341.  
  342.                     /* Disabled.
  343.                      */
  344.                     SetWindowTitles(win, "Disabled", (UBYTE *)~0);
  345.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  346.                         GA_Disabled, TRUE,
  347.                         GA_ReadOnly, FALSE,
  348.                         TAG_DONE);
  349.                     wait_for_close(win, listbrowser_gad);
  350.  
  351.                     /* No scrollbars, borderless.
  352.                      */
  353.                     SetWindowTitles(win, "No scrollbars, borderless", (UBYTE *)~0);
  354.                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  355.                         GA_Disabled, FALSE,
  356.                         GA_Top, win->BorderTop,
  357.                         GA_Left, 2,
  358.                         GA_RelWidth, -18,
  359.                         GA_RelHeight, -(win->BorderTop + win->BorderBottom),
  360.                         LISTBROWSER_Borderless, TRUE,
  361.                         LISTBROWSER_HorizontalProp, FALSE,
  362.                         LISTBROWSER_VerticalProp, FALSE,
  363.                         TAG_DONE);
  364.                     wait_for_close(win, listbrowser_gad);
  365.  
  366.                     RemoveGList(win, listbrowser_gad, -1);
  367.                     DisposeObject(listbrowser_gad);
  368.                 }
  369.                 else
  370.                     PutStr("ERROR: Couldn't create ListBrowser gadgetn");
  371.  
  372.                 free_list(&label_list);
  373.  
  374.                 PutStr("Freeing ListBrowser class\n");
  375.                 CloseLibrary(ListBrowserBase);
  376.             }
  377.             else
  378.                 PutStr("ERROR: Couldn't create ListBrowser class\n");
  379.  
  380.             CloseWindow(win);
  381.         }
  382.         else
  383.             PutStr("ERROR: Couldn't open window\n");
  384.  
  385.         UnlockPubScreen(0, screen);
  386.     }
  387.     else
  388.         PutStr("ERROR: Couldn't lock public screen\n");
  389. }
  390.  
  391.  
  392. /* Function to make a List of ListBrowserNodes from a couple of arrays.
  393.  * Just to demonstrate things, we make make three columns, 2 with text
  394.  * (the same text) and the third with numbers.
  395.  */
  396. BOOL make_list(struct List *list, UBYTE **labels1, LONG *labels2)
  397. {
  398.     struct Node *node;
  399.     WORD i = 0;
  400.  
  401.     NewList(list);
  402.  
  403.     while (*labels1)
  404.     {
  405.         if (node = AllocListBrowserNode(3,
  406.                         LBNA_Column, 0,
  407.                             LBNCA_CopyText, TRUE,
  408.                             LBNCA_Text, *labels1,
  409.                             LBNCA_MaxChars, 40,
  410.                             LBNCA_Editable, TRUE,
  411.                         LBNA_Column, 1,
  412.                             LBNCA_CopyText, TRUE,
  413.                             LBNCA_Text, *labels1,
  414.                             LBNCA_MaxChars, 40,
  415.                             LBNCA_Editable, TRUE,
  416.                         LBNA_Column, 2,
  417.                             LBNCA_Integer, &labels2[i],
  418.                             LBNCA_Justification, LCJ_RIGHT,
  419.                         TAG_DONE))
  420.         {
  421.             AddTail(list, node);
  422.         }
  423.         else
  424.             break;
  425.  
  426.         labels1++;
  427.         i++;
  428.     }
  429.     return(TRUE);
  430. }
  431.  
  432.  
  433. /* Function to free an Exec List of ListBrowser nodes.
  434.  */
  435. VOID free_list(struct List *list)
  436. {
  437.     struct Node *node, *nextnode;
  438.  
  439.     node = list->lh_Head;
  440.     while (nextnode = node->ln_Succ)
  441.     {
  442.         FreeListBrowserNode(node);
  443.         node = nextnode;
  444.     }
  445.     NewList(list);
  446. }
  447.  
  448.  
  449. /* Wait for the window close gadget to be pressed.
  450.  */
  451. VOID wait_for_close(struct Window *win, struct Gadget *listbrowser_gad)
  452. {
  453.     BOOL ok = TRUE;
  454.  
  455.     /* Just wait around until the close gadget is pressed.
  456.      */
  457.     while (ok)
  458.     {
  459.         struct Gadget *gadget;
  460.         struct IntuiMessage *imsg;
  461.             
  462.         WaitPort(win->UserPort);
  463.         while (imsg = (struct IntuiMessage *)GetMsg(win->UserPort))
  464.         {
  465.             switch (imsg->Class)
  466.             {
  467.                 case IDCMP_CLOSEWINDOW:
  468.                     ok = FALSE;
  469.                     break;
  470.  
  471.                 case IDCMP_RAWKEY:
  472.                     if (!(imsg->Code & IECODE_UP_PREFIX))
  473.                     {
  474.                         switch (imsg->Code)
  475.                         {
  476.                             case RAWKEY_CURSORUP:
  477.                                 if (imsg->Qualifier & QUALIFIER_CTRL)
  478.                                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  479.                                         LISTBROWSER_Position, LBP_TOP,
  480.                                         TAG_DONE);
  481.                                 if (imsg->Qualifier & QUALIFIER_SHIFT)
  482.                                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  483.                                         LISTBROWSER_Position, LBP_PAGEUP,
  484.                                         TAG_DONE);
  485.                                 else
  486.                                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  487.                                         LISTBROWSER_Position, LBP_LINEUP,
  488.                                         TAG_DONE);
  489.                                 break;
  490.  
  491.                             case RAWKEY_CURSORDOWN:
  492.                                 if (imsg->Qualifier & QUALIFIER_CTRL)
  493.                                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  494.                                         LISTBROWSER_Position, LBP_BOTTOM,
  495.                                         TAG_DONE);
  496.                                 if (imsg->Qualifier & QUALIFIER_SHIFT)
  497.                                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  498.                                         LISTBROWSER_Position, LBP_PAGEDOWN,
  499.                                         TAG_DONE);
  500.                                 else
  501.                                     SetGadgetAttrs(listbrowser_gad, win, NULL,
  502.                                         LISTBROWSER_Position, LBP_LINEDOWN,
  503.                                         TAG_DONE);
  504.                                 break;
  505.  
  506.                             default:
  507.                                 break;
  508.                         }
  509.                     }
  510.                     break;
  511.  
  512.                 case IDCMP_GADGETUP:
  513.                     gadget = (struct Gadget *)imsg->IAddress;
  514.                     Printf("Gadget: %ld  Code: %ld\n",
  515.                         (LONG)gadget->GadgetID, (LONG)imsg->Code);
  516.  
  517.                     break;
  518.         
  519.                 default:
  520.                     break;
  521.             }
  522.             ReplyMsg((struct Message *)imsg);
  523.         }
  524.     }
  525. }
  526.